/*
 * @Author: szx
 * @Date: 2022-02-28 15:19:24
 * @LastEditTime: 2022-02-28 15:31:54
 * @Description:
 * @FilePath: \leetcode\剑指offer\12\12.js
 */
/**
 * @param {number[]} nums
 * @return {number}
 */
 var pivotIndex = function (nums) {
    let sum = nums.reduce((a, b) => a + b, 0);
    let tmp = 0;
    for (let i = 0; i < nums.length; i++) {
        const left = sum - nums[i];
        if ((left & 1) == 0 && tmp == Math.floor(left / 2)) return i;
        tmp += nums[i];
    }
    return -1;
};
console.log(pivotIndex([1, 7, 3, 6, 5, 6]));
console.log(pivotIndex([1, 2, 3]));
console.log(pivotIndex([2, 1, -1]));
console.log(pivotIndex([1, -1, 2]));
console.log(pivotIndex([-1, -1, -1, 1, 1, 1]));
